import { createStore, useStore as useBaseStore } from "vuex";
import type { Store } from "vuex";
import type { InjectionKey } from "vue";

import modules from "./modules";

// ts支持的文档：https://vuex.vuejs.org/zh/guide/typescript-support.html

// counter模块的类型
export interface Counter {
  count: number;
}

// vuex管理的所有状态数据的类型
export interface State {
  counter: Counter;
}

// 定义 injection key
export const key: InjectionKey<Store<State>> = Symbol();

export default createStore<State>({
  // state: {},
  // getters: {},
  // actions: {},
  // mutations: {},
  modules,
});

// 定义自己的 `useStore` 组合式函数, 简化使用useStore
export function useStore() {
  return useBaseStore(key);
}
